import org.openqa.selenium.By;

import java.io.IOException;

/**
 * @projectName: The-examination-questionnaire-pass-test
 * @package: PACKAGE_NAME
 * @className: LoginPage
 * @author: Tongxinxin
 * @description: 登录界面测试
 * @date: 2025/9/22 0:27
 * @version: 1.0
 */
public class LoginPage extends Util{
    public static String url="http://8.155.1.153:8081/user/login";

    public LoginPage(String url) {
        super(url);
    }

    //检查页面是否能正常显示
    public void LoginPage() throws InterruptedException{
        //检查是否有登录按钮
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.top___1C1Zi > div.header___5xZ3f > span"));
        //用户名输入框
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div:nth-child(2) > div > div > div > div > span"));
    }

    //登录成功
    public void LoginSuc(){
        driver.findElement(By.cssSelector("#username")).sendKeys("tong");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        //查看是否登录成功
        driver.findElement(By.cssSelector("#sk-layout > div > div > section > div.ant-pro-layout-container > main > div > div.ant-page-header.ant-pro-page-container-warp-page-header.ant-pro-page-container-warp-page-header.ant-page-header-has-breadcrumb.ant-page-header-ghost > div.ant-page-header-heading > div > span"));
        //通过页面标签来检查是否登录成功
        String expect=driver.getTitle();
        assert expect.equals("首页");
    }

    //登录失败--输入错误的用户名和密码
    public void LoginFail1() throws IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("tong11less");
        driver.findElement(By.cssSelector("#password")).sendKeys("123123");
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        //登陆失败
        String res=driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div")).getText();

        getScreenShot(getClass().getName());

        assert res.equals("错误的用户名和密码");
    }

    //登录失败---输入错误的用户名和正确的密码
    public void LoginFail2() throws IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("tong11lessss");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        //登陆失败
        String res=driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div")).getText();

        getScreenShot(getClass().getName());

        assert res.equals("错误的用户名和密码");
    }

    //登录失败---输入正确的用户名和错误的密码
    public void LoginFail3() throws IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("tong");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456789");
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        //登陆失败
        String res=driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div")).getText();

        getScreenShot(getClass().getName());

        assert res.equals("错误的用户名和密码");
    }

    //登陆失败--不输入用户名，输入密码
    public void LoginFail4() throws IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        //登陆失败
        String res=driver.findElement(By.cssSelector("#username_help > div")).getText();

        getScreenShot(getClass().getName());

        assert res.equals("用户名是必填项");
    }

    //登陆失败--不输入密码，输入用户名
    public void LoginFail5() throws IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("tong");
        driver.findElement(By.cssSelector("#password")).sendKeys("");
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        //登陆失败
        String res=driver.findElement(By.cssSelector("#password_help > div")).getText();
        getScreenShot(getClass().getName());
        assert res.equals("密码是必填项");
    }
}
